﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using Microsoft.Data.Edm;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class JobInfoAllController : Controller
    {
        //
        // GET: /JobInfoAll/

        public ActionResult Index(int id = 0, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
           // GswTblJobInfo.Update("set JobState=2 where EndDate<'" + DateTime.Today.ToShortDateString() + "'");
            var qry = GswTblJobInfoAll.GetJobInfoAll(sdate, edate, jname, GswTblUser.CurrentUser.CustomerId,  pageIndex);

            return View(qry);
        }
        public ActionResult Create(int id = 0, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
          
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var jobInfo = id == 0 ? new GswTblJobInfoAll { PubDate = DateTime.Today, EndDate = DateTime.Today, ParentId = GswTblUser.CurrentUser.CustomerId } : GswTblJobInfoAll.SingleOrDefault(id);
            jobInfo.JobBaseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " and (FunctionId=120 OR FunctionId=122 or FunctionId=130 or FunctionId=131 or FunctionId=132 ) order by FunctionName,TypeNo");
            foreach (var item in jobInfo.JobBaseInfos.Where(item => item.FunctionId == 120))
                item.BaseInfos = GswTblBaseInfo.Fetch("where MasterId=" + item.Id + " order by typeno");
            return View(jobInfo);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0, int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
           
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
          
            if (InputValid.StrToInt(c["CustomerId"],0)==0)
                ModelState.AddModelError("", "请选择招聘单位");
            if (InputValid.StrToInt(c["JobTypeId"], 0) == 0)
                ModelState.AddModelError("", "请选择职位类别");
            if(string.IsNullOrEmpty(c["JobName"]))
                ModelState.AddModelError("","职位名称不允许为空");
            if (InputValid.StrToInt(c["JobNum"], 0) == 0)
                ModelState.AddModelError("", "招聘人数必须大于0");
            if(string.IsNullOrEmpty(c["SalaryRange"]))
                ModelState.AddModelError("","薪资待遇不允许为空，请选择");
            if(string.IsNullOrEmpty(c["WorkPlace"]))
                ModelState.AddModelError("","工作地点不允许为空");
            if (InputValid.StrToInt(c["CustomerId"], 0) == 0)
                ModelState.AddModelError("","请选择招聘单位");

            var jobInfo = id == 0 ? new GswTblJobInfoAll() : GswTblJobInfoAll.SingleOrDefault(id);

            var baseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " order by FunctionName,TypeNo");
            jobInfo.PubDate = DateTime.Parse(c["PubDate"]);
            jobInfo.EndDate = DateTime.Parse(c["EndDate"]);
            jobInfo.ParentId = GswTblUser.CurrentUser.CustomerId;
            jobInfo.CustomerId = InputValid.StrToInt(c["CustomerId"], 0);
            jobInfo.JobName = c["JobName"];
            jobInfo.JobNum = InputValid.StrToInt(c["JobNum"], 0);
            jobInfo.WorkPlace = c["WorkPlace"];
            jobInfo.JobWelfare = c["JobWelfare"];
            jobInfo.JobDuty = c["JobDuty"];
            jobInfo.JobDesc = c["JobDesc"];
            jobInfo.JobReq = c["JobReq"];
            jobInfo.SalaryRange = c["SalaryRange"];
            jobInfo.JobTypeId = InputValid.StrToInt(c["JobTypeId"], 0);
            jobInfo.JobType = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["JobTypeId"], 0));
            jobInfo.EduBackground = c["EduBackground"];
            jobInfo.WorkExpe = c["WorkExpe"];
            jobInfo.GenderReq = c["GenderReq"];
            jobInfo.LangReq = "";
           
            jobInfo.ReFlag = InputValid.StrToInt(c["ReFlag"], 0);
            jobInfo.SourceFlag = 0;
            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {

                        jobInfo.SourceFlag = 1;
                        jobInfo.Insert();
                        jobInfo = new GswTblJobInfoAll { PubDate = DateTime.Today, EndDate = DateTime.Today, ParentId = GswTblUser.CurrentUser.CustomerId };

                    }
                    else
                    {
                        jobInfo.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            jobInfo.JobBaseInfos = GswTblBaseInfo.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " and (FunctionId=120 OR FunctionId=122 or FunctionId=130 or FunctionId=131 or FunctionId=132 ) order by FunctionName,TypeNo");
            foreach (var item in jobInfo.JobBaseInfos.Where(item => item.FunctionId == 120))
                item.BaseInfos = GswTblBaseInfo.Fetch("where MasterId=" + item.Id + " order by typeno");
          
            return View(jobInfo);

        }

        public ActionResult Delete(int id , int pageIndex = 1, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string jname = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.jname = jname;
            ViewBag.pageIndex = pageIndex;
            
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblJobInfoAll.Delete(id);
           
            var qry = GswTblJobInfoAll.GetJobInfoAll(sdate, edate, jname,  GswTblUser.CurrentUser.CustomerId, pageIndex);

            return View("Index",qry);
        }


    }
}
